package com.Smart.smartscals.HttpTools;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;

/**
 * 本类为解析json数据的相关类
 * @author Administrator
 *
 */
public class ParserJsonTools {
	private static JSONTokener jsonParser;
	private  static Object JSONObject;
	/**
	 * 每次得到了HttpResponse的时候才都要先调用此方法，得到状态码，看是否请求成功
	 * 
	 * @param response
	 * @return 请求是否成功
	 */
	public boolean getStatusCode(HttpResponse response){
		int statusCode = 0 ;
		statusCode = response.getStatusLine().getStatusCode();
		if(statusCode==200){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 此方法在确定请求成功（返回状态码为200时）的时候调用 
	 * @param response 响应实体
	 * @return json 字符串
	 */

	public String getResponseJson(HttpResponse response){
		String jsonString = null ;
		try {
			InputStream in = response.getEntity().getContent();
			byte[] data = new byte[1024];
			int len = 0;
			ByteArrayOutputStream out = new ByteArrayOutputStream();
			while((len = in.read(data)) != -1){
				out.write(data,0,len);
			}
			jsonString =new String(out.toByteArray(),"UTF-8");
			if(in != null){
				in.close();
			}
			if(out != null){
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonString;
	}
	/**
	 * 用于解析登录的时候返回的json数据
	 * @return hashMap
	 */
	public static HashMap<String, String> parserLoginJson(String JSON){
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			jsonParser = new JSONTokener(JSON);
			JSONObject = jsonParser.nextValue();
			String isSuccess = ((org.json.JSONObject) JSONObject).getString("success");
			if(isSuccess.equals("true")){
				//登录成功，并把authkey解析出来
				map.put("success", isSuccess);
				map.put("authkey", ((org.json.JSONObject) JSONObject).getString("authkey"));
			}else {
				//登录失败时返回错误信息
				map.put("success",isSuccess);
				map.put("errormessage",((org.json.JSONObject) JSONObject).getString("errormessage"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 用于解析注册的时候返回的json数据
	 * @param JSON
	 * @return hashMap
	 */
	public static HashMap<String, String> parserRegistJson(String JSON){
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			jsonParser = new JSONTokener(JSON);
			JSONObject = jsonParser.nextValue();
			String isSuccess = ((org.json.JSONObject) JSONObject).getString("success");
			if(isSuccess.equals("true")){
				//注册成功，并把authkey解析出来
				map.put("success", isSuccess);
				map.put("authkey", ((org.json.JSONObject) JSONObject).getString("authkey"));
			}else {
				//注册失败时返回错误信息
				map.put("success",isSuccess);
				map.put("errormessage",((org.json.JSONObject) JSONObject).getString("errormessage"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return map ;
	}
	/**
	 * 用于解析上传数据后返回的Json
	 * @param JSON
	 * @return
	 */
	public HashMap<String, String> parserUploadJson(String JSON){
		HashMap<String, String> map = new HashMap<String, String>();
		try {
			jsonParser = new JSONTokener(JSON);
			JSONObject = jsonParser.nextValue();
			String isSuccess = ((org.json.JSONObject) JSONObject).getString("success");
			if(isSuccess.equals("true")){
				//上传成功，并把authkey解析出来
				map.put("success", isSuccess);
			}else {
				//上传失败时返回错误信息（可能是authkey不合法）
				map.put("success",isSuccess);
				map.put("errormessage",((org.json.JSONObject) JSONObject).getString("errormessage"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	public HashMap<String, String> parserBodyData(String JSON){
		HashMap map = new HashMap<String, String>();
		try {
			JSONObject jsonObject = new JSONObject(JSON);
			JSONArray jsonArray = (JSONArray) jsonObject.get("bodycomposition");
			for (int i = 0; i < jsonArray.length(); ++i) {
				JSONObject o = (JSONObject) jsonArray.get(i);
				System.out.println("visceralfat:" + o.getString("visceralfat") + "," + "weight:"
						+ o.getInt("weight"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return map;
	}

}
